home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / windows / games / wmfread.arj / WMFMETA.C < prev    next >
C/C++ Source or Header  |  1992-11-17  |  43KB  |  1,374 lines

  1. /***********************************************************************
  2.  
  3.   MODULE     : WMFMETA.C
  4.  
  5.   FUNCTIONS  : MetaEnumProc
  6.                GetMetaFileAndEnum
  7.                LoadParameterLB
  8.                PlayMetaFileToDest
  9.                RenderClipMeta
  10.                RenderPlaceableMeta
  11.                SetPlaceableExts
  12.                SetClipMetaExts
  13.                ProcessFile
  14.  
  15.   COMMENTS   :
  16.  
  17. ************************************************************************/
  18. // COPYRIGHT:
  19. //
  20. //   (C) Copyright Microsoft Corp. 1992.  All rights reserved.
  21. //
  22. //   You have a royalty-free right to use, modify, reproduce and
  23. //   distribute the Sample Files (and/or any modified version) in
  24. //   any way you find useful, provided that you agree that
  25. //   Microsoft has no warranty obligations or liability for any
  26. //   Sample Application Files which are modified.
  27. #include "windows.h"
  28. #include "wmfdcode.h"
  29. #include "stdlib.h"
  30.  
  31. /***********************************************************************
  32.  
  33.   FUNCTION   : MetaEnumProc
  34.  
  35.   PARAMETERS : HDC           hDC
  36.                LPHANDLETABLE lpHTable
  37.                LPMETARECORD  lpMFR
  38.                int           nObj
  39.                BYTE FAR *    lpClientData
  40.  
  41.  
  42.   PURPOSE    : callback for EnumMetaFile.  Handles the stepping of
  43.                each metafile record.
  44.  
  45.   CALLS      : WINDOWS
  46.                  GlobalAlloc
  47.                  GlobalUnlock
  48.                  GlobalFree
  49.                  MakeProcInstance
  50.                  DialogBox
  51.                  FreeProcInstance
  52.                  MessageBox
  53.                  lstrcat
  54.                  SendDlgItemMessage
  55.                  PlayMetaFileRecord
  56.  
  57.  
  58.   MESSAGES   : none
  59.  
  60.   RETURNS    : int
  61.  
  62.   COMMENTS   : ENUMMFSTEP is used whenever records are to be played,
  63.                regardless of whether you are playing records from the
  64.                list, stepping all, or stepping a range.
  65.  
  66.                ENUMMFLIST is used when you need to add strings to a listbox
  67.                that describe the type of reocrd.
  68.  
  69.   HISTORY    : 1/16/91 - created - drc
  70.  
  71. ************************************************************************/
  72.  
  73.  
  74. int FAR PASCAL MetaEnumProc (hDC, lpHTable, lpMFR, nObj, lpClientData)
  75. HDC hDC;
  76. LPHANDLETABLE lpHTable;
  77. LPMETARECORD lpMFR;
  78. int nObj;
  79. BYTE FAR *lpClientData;
  80. {
  81.    WORD i;
  82.    char szMetaFunction[50];
  83.    static BOOL iMapModeSet = FALSE;
  84.    BOOL DlgRet;
  85.  
  86.    /* what is the enumeration action that we are taking? */
  87.  
  88.    switch (iEnumAction)
  89.       {
  90.  
  91.    /* if the enumeration was entered ala the step metafile menu selection */
  92.    case ENUMMFSTEP:
  93.  
  94.       /* keep track of the current metafile record number */
  95.       iRecNum++;
  96.  
  97.       /* allocate memory for the record.  this memory will be used by
  98.          other functions that need to use the contents of the record */
  99.       hMem = GlobalAlloc(GHND, (LONG)2 * lpMFR->rdSize);
  100.  
  101.       /* if the memory was successfully allocated */
  102.       if (hMem)
  103.       {
  104.  
  105.          /* obtain a long pointer to this memory */
  106.          lpMFParams = (LPPARAMETERS)GlobalLock(hMem);
  107.  
  108.          /* copy the contents of the record to the global memory */
  109.          MetaRec.rdSize = lpMFR->rdSize;
  110.          MetaRec.rdFunction = lpMFR->rdFunction;
  111.          for (i = 0; (DWORD)i < lpMFR->rdSize - 3; i++)
  112.             *lpMFParams++ = lpMFR->rdParm[i];
  113.          GlobalUnlock(hMem);
  114.  
  115.          /* if STEPPING through metafile records that have been selected
  116.             by selecting the menu options Play - Step - All, Play - Step -
  117.             Range, or selecting records from the View - List listbox */
  118.          if (!bPlayItAll || (bEnumRange && iRecNum >= (WORD)iStartRange &&
  119.              iRecNum <= (WORD)iEndRange) || (bPlayList && !bPlayItAll))
  120.          {
  121.  
  122.             /* if playing records selected from the View - List
  123.                listbox of records */
  124.             if (bPlayList)
  125.             {
  126.  
  127.                /* if playing the selected records */
  128.                if (bPlaySelList)
  129.                {
  130.  
  131.                   /* if done playing the selected records then stop the
  132.                      enumeration */
  133.                   if (iCount == iNumSel)
  134.                      return (0);
  135.  
  136.                   /* if this is a selected record then play it */
  137.                   if ((WORD)lpSelMem[iCount] == iRecNum - 1)
  138.                   {
  139.  
  140.                      /* initialize flag */
  141.                      bPlayRec = FALSE;
  142.  
  143.                      /* increment the count */
  144.                      iCount = (iCount < iLBItemsInBuf) ? iCount++ : iCount;
  145.  
  146.                      /* call the dialog box that lets you play or ignore this record */
  147.                      lpWMFRecDlgProc = MakeProcInstance((FARPROC)WMFRecDlgProc,
  148.                                                         hInst);
  149.                      DlgRet = DialogBox(hInst, (LPSTR)"WMFDLG", hWndMain,
  150.                                         lpWMFRecDlgProc);
  151.                      FreeProcInstance((FARPROC)lpWMFRecDlgProc);
  152.                   }
  153.                   else
  154.                      /* initialize flag and do nothing else */
  155.                      bPlayRec = FALSE;
  156.                }
  157.                /* playing the unselected records */
  158.                else
  159.                {
  160.  
  161.                   /* if this is one of the selected records then increment
  162.                      the record count and init a flag but do nothing else */
  163.                   if ((WORD)lpSelMem[iCount] == iRecNum - 1)
  164.                   {
  165.  
  166.                      /* set count to next selected record in listbox */
  167.                      iCount = (iCount < iLBItemsInBuf) ? iCount++ : iCount;
  168.                      bPlayRec = FALSE;
  169.                   }
  170.  
  171.                   /* this is not one of the selected records which is what we
  172.                      want in this case.  So, init a flag give the user the
  173.                      opportunity to play the record */
  174.                   else
  175.                   {
  176.                      bPlayRec = FALSE;
  177.                      lpWMFRecDlgProc = MakeProcInstance((FARPROC)WMFRecDlgProc,
  178.                                                         hInst);
  179.                      DlgRet = DialogBox(hInst, (LPSTR)"WMFDLG", hWndMain,
  180.                                         lpWMFRecDlgProc);
  181.                      FreeProcInstance((FARPROC)lpWMFRecDlgProc);
  182.                   }
  183.                }
  184.             } /* bPlayList */
  185.  
  186.             /* stepping records from the Play - Step menu option */
  187.             else
  188.             {
  189.  
  190.                /* init a flag and show the record contents */
  191.                bPlayRec = FALSE;
  192.                iCount = (iCount < iLBItemsInBuf) ? iCount++ : iCount;
  193.                lpWMFRecDlgProc = MakeProcInstance((FARPROC)WMFRecDlgProc, hInst
  194.                                                   );
  195.                DlgRet = DialogBox(hInst, (LPSTR)"WMFDLG", hWndMain,
  196.                                   lpWMFRecDlgProc);
  197.                FreeProcInstance((FARPROC)lpWMFRecDlgProc);
  198.             }
  199.          }
  200.  
  201.          /* bPlayItAll is TRUE.  This is set when the user either
  202.             selects the menu option Play - All or pushes the GO button
  203.             in the view record dialog box */
  204.          else
  205.          {
  206.  
  207.             /* we were stepping records selected from the listbox and
  208.                the user pressed the GO button
  209.  
  210.            Don't bother returning 0 to stop enumeration.  We need to
  211.            play to the end of the metafile in this case anyway */
  212.             if (bPlayList)
  213.             {
  214.  
  215.                /* we were playing the selected records */
  216.                if (bPlaySelList)
  217.                {
  218.  
  219.                   /* if all of the selected records have been played then
  220.                      stop the enumeration */
  221.                   if (iCount == iNumSel)
  222.                      return (0);
  223.  
  224.                   /* set bPlayRec so the record will be played without user
  225.                      interation and then update the record counter */
  226.                   if ((WORD)lpSelMem[iCount] == iRecNum - 1)
  227.                   {
  228.                      bPlayRec = TRUE;
  229.                      iCount = (iCount < iLBItemsInBuf) ? iCount++ : iCount;
  230.                   }
  231.                   else
  232.                      /* it wasn't one of the selected records so don't play */
  233.                      bPlayRec = FALSE;
  234.                }
  235.                /* we were playing the unselected records */
  236.                else
  237.                {
  238.  
  239.                   /* if it is a selected record then set bPlayRec to FALSE
  240.                      so the record is not played */
  241.                   if ((WORD)lpSelMem[iCount] == iRecNum - 1)
  242.                   {
  243.                      bPlayRec = FALSE;
  244.                      iCount = (iCount < iLBItemsInBuf) ? iCount++ : iCount;
  245.                   }
  246.                   else
  247.                      /* play the record */
  248.                      bPlayRec = TRUE;
  249.                }
  250.             }
  251.          }
  252.  
  253.  
  254.          /* Stop the enumeration if you were stepping a range and have
  255.             finished playing that range OR the user selected pushed
  256.             the STOP button in the view record dialog box */
  257.          if (((bEnumRange) && (iRecNum > (WORD)iEndRange)) || (!DlgRet))
  258.          {
  259.             bPlayRec = FALSE;
  260.             return (0); //stop enumeration
  261.          }
  262.       } /* hMem */
  263.       else
  264.          /* we were unable to allocate memory for the record */
  265.          MessageBox(hWndMain, "Memory allocation failed", NULL, MB_OK |
  266.                     MB_ICONHAND);
  267.  
  268.       /* Regardless of the method the user elected to play the
  269.          records, check the flag.  If it is set then play the
  270.          record */
  271.       if (bPlayRec)
  272.          PlayMetaFileRecord(hDC, lpHTable, lpMFR, (WORD)nObj);
  273.  
  274.       /* done with the record so get rid of it */
  275.       GlobalFree(hMem);
  276.  
  277.       /* if we made it this far then continue the enumeration */
  278.       return (1);
  279.       break;
  280.  
  281.    case ENUMMFLIST:
  282.       iRecNum++;
  283.  
  284.       /* format the listbox string */
  285.       wsprintf((LPSTR)szMetaFunction, (LPSTR)"%d - ", iRecNum);
  286.  
  287.       /* get the function number contained in the record */
  288.       MetaRec.rdFunction = lpMFR->rdFunction;
  289.  
  290.       /* lookup the function number in the structure MetaFunctions */
  291.       for (i = 0; i < NUMMETAFUNCTIONS; i++)
  292.       {
  293.          if ((DWORD)lpMFR->rdFunction == MetaFunctions[i].value)
  294.             break;
  295.       }
  296.  
  297.       /* if the function number is not found then describe this record
  298.          as an "Unknown" type otherwise use the corresponding name
  299.          found in the lookup */
  300.       if (MetaRec.rdFunction != MetaFunctions[i].value)
  301.          lstrcat((LPSTR)szMetaFunction, (LPSTR)"Unknown");
  302.       else
  303.          lstrcat((LPSTR)szMetaFunction, (LPSTR)MetaFunctions[i].szFuncName);
  304.  
  305.       /* add the string to the listbox */
  306.       SendDlgItemMessage(CurrenthDlg, IDL_LBREC, LB_ADDSTRING, 0, (LONG)(LPSTR)
  307.                          szMetaFunction);
  308.  
  309.       /* keep enumerating */
  310.       return (1);
  311.       break;
  312.       }
  313. }
  314.  
  315. /***********************************************************************
  316.  
  317.   FUNCTION   : GetMetaFileAndEnum
  318.  
  319.   PARAMETERS : HDC hDC
  320.  
  321.   PURPOSE    : load the metafile if it has not already been loaded and
  322.                begin enumerating it
  323.  
  324.   CALLS      : WINDOWS
  325.                  GetMetaFile
  326.                  MakeProcInstance
  327.                  EnumMetaFile
  328.                  FreeProcInstance
  329.                  DeleteMetaFile
  330.                  MessageBox
  331.  
  332.   MESSAGES   : none
  333.  
  334.   RETURNS    : void
  335.  
  336.   COMMENTS   :
  337.  
  338.   HISTORY    : 1/16/91 - created - drc
  339.  
  340. ************************************************************************/
  341.  
  342.  
  343. void GetMetaFileAndEnum (hDC)
  344. HDC hDC;
  345. {
  346.    /* if the metafile has not already been rendered as a placeable
  347.       or clipboard metafile then open it using GDI */
  348.    if (!bMetaInRam)
  349.       hMF = GetMetaFile((LPSTR)OpenName);
  350.  
  351.    /* if there is a valid handle to a metafile begin enumerating it */
  352.    if (hMF)
  353.    {
  354.       iEnumAction = ENUMMFSTEP;
  355.       lpprocEnumMF = MakeProcInstance((FARPROC)MetaEnumProc, hInst);
  356.       EnumMetaFile(hDC, hMF, lpprocEnumMF, (LPARAM)NULL);
  357.       FreeProcInstance((FARPROC)lpprocEnumMF);
  358.  
  359.       /* if this metafile was loaded using GetMetaFile delete it as we
  360.          are done with it now */
  361.       if (!bMetaInRam)
  362.          hMF = DeleteMetaFile(hMF);
  363.    }
  364.    else
  365.       MessageBox(hWndMain, "GetMetaFile failed", NULL, MB_OK | MB_ICONHAND);
  366.    return;
  367. }
  368.  
  369. /***********************************************************************
  370.  
  371.   FUNCTION   : LoadParameterLB
  372.  
  373.   PARAMETERS : HWND  hDlg
  374.            DWORD dwParams
  375.            int   nRadix - HEX to display contents in base 16
  376.                   DEC to display contents in base 10
  377.  
  378.   PURPOSE    : display the parameters of the metafile record in
  379.            the parameter listbox
  380.  
  381.   CALLS      : WINDOWS
  382.          GlobalLock
  383.          GlobalUnlock
  384.          SendDlgItemMessage
  385.          wsprintf
  386.          lstrlen
  387.  
  388.   MESSAGES   : WM_SETREDRAW
  389.            WM_RESETCONTENT
  390.            LB_ADDSTRING
  391.  
  392.   RETURNS    : BOOL
  393.  
  394.   COMMENTS   :
  395.  
  396.   HISTORY    : 1/16/91 - created - drc
  397.  
  398. ************************************************************************/
  399.  
  400.  
  401. BOOL LoadParameterLB (hDlg, dwParams, nRadix)
  402. HWND hDlg;
  403. DWORD dwParams;
  404. int nRadix;
  405. {
  406.    DWORD i;
  407.    BYTE nHiByte, nLoByte;
  408.    char szBuffer[8];
  409.    char szDump[100];
  410.    int iValue;
  411.  
  412.    switch (nRadix)  /* if nRadix is not a valid value, return FALSE */
  413.       {
  414.    case IDB_HEX:
  415.  
  416.    case IDB_DEC:
  417.  
  418.    case IDB_CHAR:
  419.       break;
  420.  
  421.    default:
  422.       return FALSE;
  423.       }
  424.  
  425.    /* lock the memory where the parameters can be found */
  426.    if (NULL == (lpMFParams = (LPPARAMETERS)GlobalLock(hMem)))
  427.       return (FALSE);
  428.  
  429.    /* init the strings */
  430.    *szDump = '\0';
  431.  
  432.    /* turn off redrawing of the listbox */
  433.    SendDlgItemMessage(hDlg, IDL_PARAMETERS, WM_SETREDRAW, FALSE, 0L);
  434.  
  435.    /* reset the contents of the listbox */
  436.    SendDlgItemMessage(hDlg, IDL_PARAMETERS, LB_RESETCONTENT, 0, 0L);
  437.  
  438.    /* loop through the metafile record parameters */
  439.    for (i = 0; i < dwParams; i++)
  440.    {
  441.  
  442.       /* get the high and low byte of the parameter word */
  443.       nHiByte = HIBYTE(lpMFParams[i]);
  444.       nLoByte = LOBYTE(lpMFParams[i]);
  445.       switch (nRadix)
  446.          {
  447.       case IDB_HEX: /* if we are to display as hexadecimal */
  448.          /* format the bytes for the hex part of dump */
  449.          wsprintf((LPSTR)szBuffer, (LPSTR)"%02x %02x ", nLoByte, nHiByte);
  450.          break;
  451.  
  452.       case IDB_DEC:
  453.          /* format the bytes for the decimal part of dump */
  454.          iValue = lpMFParams[i];
  455.          wsprintf((LPSTR)szBuffer, (LPSTR)"%d ", iValue);
  456.          break;
  457.  
  458.       case IDB_CHAR:
  459.          wsprintf((LPSTR)szBuffer, (LPSTR)"%c%c", (nLoByte > 0x20) ? nLoByte :
  460.                   0x2E, (nHiByte > 0x20) ? nHiByte : 0x2E);
  461.          break;
  462.  
  463.       default:
  464.          return FALSE;
  465.          }
  466.  
  467.  
  468.       /* concatenate it onto whatever we have already formatted */
  469.       lstrcat((LPSTR)szDump, (LPSTR)szBuffer);
  470.  
  471.       /* use every 8 words for hex/dec dump */
  472.       if (!((i + 1) % 8))
  473.       {
  474.  
  475.          /*add the string to the listbox */
  476.          SendDlgItemMessage(hDlg, IDL_PARAMETERS, LB_ADDSTRING, 0, (LONG)(LPSTR
  477.                             )szDump);
  478.  
  479.          /* re-init the hex/dec strings in preparation for next 8 words */
  480.          *szDump = '\0';
  481.       }
  482.    }
  483.  
  484.    /* dump any leftover hex/dec dump */
  485.    if (lstrlen((LPSTR)szDump))
  486.       SendDlgItemMessage(hDlg, IDL_PARAMETERS, LB_ADDSTRING, 0, (LONG)(LPSTR)
  487.                          szDump);
  488.  
  489.    /* enable redraw to the listbox */
  490.    SendDlgItemMessage(hDlg, IDL_PARAMETERS, WM_SETREDRAW, TRUE, 0L);
  491.  
  492.    /* redraw it */
  493.    InvalidateRect(GetDlgItem(hDlg, IDL_PARAMETERS), NULL, TRUE);
  494.  
  495.    /* unlock the memory used for the parameters */
  496.    GlobalUnlock(hMem);
  497.    return (TRUE);
  498. }
  499.  
  500. /***********************************************************************
  501.  
  502.   FUNCTION   : PlayMetaFileToDest
  503.  
  504.   PARAMETERS : HWND hWnd
  505.                int  nDest - DC to play metafile to
  506.                  DESTDISPLAY - play to the display
  507.                  DESTMETA    - play into another metafile
  508.  
  509.   PURPOSE    : begin the enumeration of the metafile to the user selected
  510.                destination.  Perform the housekeeping needs appropriate
  511.                to that destination.
  512.  
  513.   CALLS      : WINDOWS
  514.                  GetClientRect
  515.                  InvalidateRect
  516.                  GetDC
  517.                  SetMapMode
  518.                  OpenFileDialog
  519.                  MessageBox
  520.                  CreateMetaFile
  521.                  DeleteMetaFile
  522.                  CloseMetaFile
  523.  
  524.                APP
  525.                  WaitCursor
  526.                  SetClipMetaExts
  527.                  SetPlaceableExts
  528.                  GetMetaFileAndEnum
  529.  
  530.   MESSAGES   : none
  531.  
  532.   RETURNS    : int
  533.  
  534.   COMMENTS   :
  535.  
  536.   HISTORY    : 1/16/91 - created - drc
  537.  
  538. ************************************************************************/
  539.  
  540.  
  541. BOOL PlayMetaFileToDest (hWnd, nDest)
  542. HWND hWnd;
  543. int nDest;
  544. {
  545.    HDC hDC;
  546.    RECT rect;
  547.    int iSaveRet;
  548.  
  549.    /* if the file opened contained a valid metafile */
  550.  
  551.    if (bValidFile)
  552.    {
  553.  
  554.       /* init the record count */
  555.       iRecNum = 0;
  556.  
  557.       /* if we are stepping the metafile then clear the client area */
  558.       if (!bPlayItAll)
  559.       {
  560.          GetClientRect(hWnd, (LPRECT)&rect);
  561.          InvalidateRect(hWnd, (LPRECT)&rect, TRUE);
  562.       }
  563.       switch (nDest)
  564.          {
  565.  
  566.       /* playing metafile to the display */
  567.       case DESTDISPLAY:
  568.          WaitCursor(TRUE);
  569.          hDC = GetDC(hWnd);
  570.  
  571.          /* metafile read in from a clipboard file */
  572.          if ((bMetaInRam) && (!bAldusMeta))
  573.             SetClipMetaExts(hDC, MFP, WMFDISPLAY);
  574.  
  575.          /* placeable metafile */
  576.          if (bAldusMeta)
  577.             SetPlaceableExts(hDC, aldusMFHeader, WMFDISPLAY);
  578.  
  579.          /* "traditional" windows metafile */
  580.          if (!bMetaInRam)
  581.             SetMapMode(hDC, MM_TEXT);
  582.  
  583.          /* begin the enumeration of the metafile */
  584.          GetMetaFileAndEnum(hDC);
  585.          ReleaseDC(hWnd, hDC);
  586.          WaitCursor(FALSE);
  587.          break;
  588.  
  589.       case DESTMETA:
  590.  
  591.          /* get a name of a file to play the metafile into */
  592.          iSaveRet = SaveFileDialog((LPSTR)SaveName);
  593.  
  594.          /* if the file selected is this metafile then warn user */
  595.          if (!lstrcmp((LPSTR)OpenName, (LPSTR)SaveName))
  596.             MessageBox(hWnd, (LPSTR)"Cannot overwrite the opened metafile!", (
  597.                        LPSTR)"Play to Metafile", MB_OK | MB_ICONEXCLAMATION);
  598.          else
  599.  
  600.           /* the user didn't hit the cancel button */ if (iSaveRet)
  601.                                                  {
  602.                                                     WaitCursor(TRUE);
  603.  
  604.                                                     /* create a disk based metafile */
  605.                                                     hDC = CreateMetaFile((LPSTR
  606.                                                                          )
  607.                                                                          SaveName
  608.                                                                          );
  609.  
  610.                                                     /* begin the enumeration of the metafile */
  611.                                                     GetMetaFileAndEnum(hDC);
  612.  
  613.                                                     /* done playing so close the metafile and delete the handle */
  614.                                                     DeleteMetaFile(
  615.                                                     CloseMetaFile(hDC));
  616.                                                     WaitCursor(FALSE);
  617.                                                  }
  618.          break;
  619.  
  620.       default:
  621.          break;
  622.          }
  623.  
  624.       /* if playing list records then free the memory used for the list of
  625.          selected records */
  626.       if (bPlayList)
  627.       {
  628.          GlobalUnlock(hSelMem);
  629.          GlobalFree(hSelMem);
  630.          bPlayList = FALSE;
  631.       }
  632.  
  633.       /* success */
  634.       return (TRUE);
  635.    }
  636.    else
  637.       /* not a valid metafile */
  638.       return (FALSE);
  639. }
  640.  
  641. /***********************************************************************
  642.  
  643.   FUNCTION   : RenderClipMeta
  644.  
  645.   PARAMETERS : CLIPFILEFORMAT    *ClipHeader
  646.                int            fh
  647.  
  648.   PURPOSE    : read metafile bits, metafilepict and metafile header
  649.                of the metafile contained within a clipboard file
  650.  
  651.   CALLS      : WINDOWS
  652.                  GlobalAlloc
  653.                  GlobalLock
  654.                  GlobalUnlock
  655.                  GlobalFree
  656.                  MessageBox
  657.                  _llseek
  658.                  _lread
  659.                  _lclose
  660.                  SetMetaFileBits
  661.  
  662.   MESSAGES   : none
  663.  
  664.   RETURNS    : BOOL
  665.  
  666.   COMMENTS   :
  667.  
  668.   HISTORY    : 1/16/91 - created - drc
  669.  
  670. ************************************************************************/
  671.  
  672.  
  673. BOOL RenderClipMeta (ClipHeader, fh)
  674. CLIPFILEFORMAT *ClipHeader;
  675. int fh;
  676. {
  677.    HANDLE hMem;
  678.    LPSTR lpMem;
  679.    HANDLE hMFP;
  680.    LPMETAFILEPICT lpMFP;
  681.    WORD wBytesRead;
  682.    WORD nSize;
  683.    LONG lOffset;
  684.  
  685.    /* allocate enough memory to read the metafile bits into */
  686.  
  687.    if (!(hMem = GlobalAlloc(GHND, ClipHeader->DataLen - sizeof(METAFILEPICT))))
  688.       return (FALSE);
  689.  
  690.    /* if unable to lock this memory then return */
  691.    if (!(lpMem = GlobalLock(hMem)))
  692.    {
  693.       GlobalFree(hMem);
  694.       return (FALSE);
  695.    }
  696.  
  697.    /* offset to the metafile bits */
  698.    lOffset = ClipHeader->DataOffset + sizeof(METAFILEPICT);
  699.    nSize = (WORD)(ClipHeader->DataLen - sizeof(METAFILEPICT));
  700.  
  701.    /* seek to the beginning of the metafile bits */
  702.    _llseek(fh, lOffset, 0);
  703.  
  704.    /* read the metafile bits */
  705.    wBytesRead = _lread(fh, lpMem, nSize);
  706.  
  707.    /* if unable to read the metafile bits return */
  708.    if (wBytesRead == -1 || wBytesRead < nSize)
  709.    {
  710.       GlobalUnlock(hMem);
  711.       GlobalFree(hMem);
  712.       MessageBox(hWndMain, "Unable to read metafile bits", NULL, MB_OK |
  713.                  MB_ICONHAND);
  714.       return (FALSE);
  715.    }
  716.  
  717.    /* return to beginning to read metafile header */
  718.    _llseek(fh, lOffset, 0);
  719.  
  720.    /* read the metafile header */
  721.    wBytesRead = _lread(fh, (LPSTR)&mfHeader, sizeof(METAHEADER));
  722.  
  723.    /* if unable to read the header return */
  724.    if (wBytesRead == -1 || wBytesRead < sizeof(METAHEADER))
  725.    {
  726.       MessageBox(hWndMain, "Unable to read metafile header", NULL, MB_OK |
  727.                  MB_ICONHAND);
  728.       return (FALSE);
  729.    }
  730.  
  731.    /* set the metafile bits to the memory allocated for that purpose */
  732.    if (NULL == (hMF = SetMetaFileBits(hMem)))
  733.    {
  734.       MessageBox(hWndMain, "Unable to set metafile bits", NULL, MB_OK |
  735.                  MB_ICONHAND);
  736.       return (FALSE);
  737.    }
  738.    GlobalUnlock(hMem);
  739.  
  740.    /* allocate memory for the metafile pict structure */
  741.    if (!(hMFP = GlobalAlloc(GHND, (DWORD)sizeof(METAFILEPICT))))
  742.    {
  743.       MessageBox(hWndMain, "Unable allocate memory for metafile pict", NULL,
  744.                  MB_OK | MB_ICONHAND);
  745.       return (FALSE);
  746.    }
  747.  
  748.    /* lock the memory */
  749.    if (!(lpMFP = (LPMETAFILEPICT)GlobalLock(hMFP)))
  750.    {
  751.       MessageBox(hWndMain, "unable to lock metafile pict memory", NULL, MB_OK |
  752.                  MB_ICONHAND);
  753.       GlobalFree(hMFP);
  754.       return (FALSE);
  755.    }
  756.  
  757.    /* reposition to the start of the METAFILEPICT header. */
  758.    _llseek(fh, ClipHeader->DataOffset, 0);
  759.  
  760.    /* read the metafile pict structure */
  761.    wBytesRead = _lread(fh, (LPSTR)&MFP, sizeof(METAFILEPICT));
  762.  
  763.    /* if unable to read, return */
  764.    if (wBytesRead == -1 || wBytesRead < sizeof(METAFILEPICT))
  765.    {
  766.       MessageBox(hWndMain, "Unable to read metafile pict", NULL, MB_OK |
  767.                  MB_ICONHAND);
  768.       return (FALSE);
  769.    }
  770.  
  771.    /* update metafile handle */
  772.    MFP.hMF = hMF;
  773.  
  774.    /* unlock the header */
  775.    GlobalUnlock(hMFP);
  776.    return (TRUE);
  777. }
  778.  
  779. /***********************************************************************
  780.  
  781.   FUNCTION   : RenderPlaceableMeta
  782.  
  783.   PARAMETERS : int fh - filehandle to the placeable metafile
  784.  
  785.   PURPOSE    : read the metafile bits, metafile header and placeable
  786.                metafile header of a placeable metafile.
  787.  
  788.   CALLS      : WINDOWS
  789.                  GlobalAlloc
  790.                  GlobalLock
  791.                  Global
  792.                  DeleteMetaFile
  793.                  SetMetaFileBits
  794.                  _llseek
  795.                  _lread
  796.                  _lclose
  797.                  MessageBox
  798.  
  799.  
  800.   MESSAGES   : none
  801.  
  802.   RETURNS    : BOOL
  803.  
  804.   COMMENTS   :
  805.  
  806.   HISTORY    : 1/16/91 - created - drc
  807.  
  808. ************************************************************************/
  809.  
  810.  
  811. BOOL RenderPlaceableMeta (fh)
  812. int fh;
  813. {
  814.    HANDLE hMem;
  815.    LPSTR lpMem;
  816.    int wBytesRead;
  817.  
  818.    /* if there is currently a metafile loaded, get rid of it */
  819.  
  820.    if ((bMetaInRam) && (hMF))
  821.       DeleteMetaFile(hMF);
  822.  
  823.    /* seek to beginning of file and read aldus header */
  824.    _llseek(fh, 0, 0);
  825.  
  826.    /* read the placeable header */
  827.    wBytesRead = _lread(fh, (LPSTR)&aldusMFHeader, sizeof(ALDUSMFHEADER));
  828.  
  829.    /* if there is an error, return */
  830.    if (wBytesRead == -1 || wBytesRead < sizeof(ALDUSMFHEADER))
  831.    {
  832.       MessageBox(hWndMain, "Unable to read placeable header", NULL, MB_OK |
  833.                  MB_ICONHAND);
  834.       return (FALSE);
  835.    }
  836.  
  837.    /* return to read metafile header */
  838.    _llseek(fh, sizeof(aldusMFHeader), 0);
  839.  
  840.    /* read the metafile header */
  841.    wBytesRead = _lread(fh, (LPSTR)&mfHeader, sizeof(METAHEADER));
  842.  
  843.    /* if there is an error return */
  844.    if (wBytesRead == -1 || wBytesRead < sizeof(METAHEADER))
  845.    {
  846.       MessageBox(hWndMain, "Unable to read metafile header", NULL, MB_OK |
  847.                  MB_ICONHAND);
  848.       return (FALSE);
  849.    }
  850.  
  851.    /* allocate memory for the metafile bits */
  852.    if (!(hMem = GlobalAlloc(GHND, (mfHeader.mtSize * 2L))))
  853.    {
  854.       MessageBox(hWndMain, "Unable to allocate memory for metafile bits", NULL,
  855.                  MB_OK | MB_ICONHAND);
  856.       return (FALSE);
  857.    }
  858.  
  859.    /* lock the memory */
  860.    if (!(lpMem = GlobalLock(hMem)))
  861.    {
  862.       MessageBox(hWndMain, "Unable to lock memory for metafile bits", NULL,
  863.                  MB_OK | MB_ICONHAND);
  864.       GlobalFree(hMem);
  865.       return (FALSE);
  866.    }
  867.  
  868.    /* seek to the metafile bits */
  869.    _llseek(fh, sizeof(aldusMFHeader), 0);
  870.  
  871.    /* read metafile bits */
  872.    wBytesRead = _lread(fh, lpMem, (WORD)(mfHeader.mtSize * 2));
  873.  
  874.    /* if there was an error */
  875.    if (wBytesRead == -1)
  876.    {
  877.       MessageBox(hWndMain, "Unable to read metafile bits", NULL, MB_OK |
  878.                  MB_ICONHAND);
  879.       GlobalUnlock(hMem);
  880.       GlobalFree(hMem);
  881.       return (FALSE);
  882.    }
  883.  
  884.    /* set the metafile bits to the memory that we allocated */
  885.    if (!(hMF = SetMetaFileBits(hMem)))
  886.       return (FALSE);
  887.    GlobalUnlock(hMem);
  888.    return (TRUE);
  889. }
  890.  
  891. /***********************************************************************
  892.  
  893.   FUNCTION   : SetPlaceableExts
  894.  
  895.   PARAMETERS : HDC           hDC
  896.                ALDUSMFHEADER ahdr
  897.                int           nDest
  898.  
  899.   PURPOSE    : set the origins and extents on the DC to correspond with
  900.                the origins and extents specified within the placeable
  901.                metafile header
  902.  
  903.   CALLS      : WINDOWS
  904.                  GetClientRect
  905.                  SetMapMode
  906.                  SetWindowOrg
  907.                  SetWindowExt
  908.                  SetViewportOrg
  909.                  SetViewportExt
  910.  
  911.                C runtime
  912.                  labs
  913.  
  914.   MESSAGES   : none
  915.  
  916.   RETURNS    : void
  917.  
  918.   COMMENTS   :
  919.  
  920.   HISTORY    : 1/16/91 - created - drc
  921.  
  922. ************************************************************************/
  923.  
  924.  
  925. void SetPlaceableExts (hDC, ahdr, nDest)
  926. HDC hDC;
  927. ALDUSMFHEADER ahdr;
  928. int nDest;
  929. {
  930.    RECT rect;
  931.  
  932.    /* if setting the extents on the display DC */
  933.  
  934.    if (nDest != WMFPRINTER)
  935.       GetClientRect(hWndMain, &rect);
  936.    SetMapMode(hDC, MM_ANISOTROPIC);
  937.  
  938.    /* set the windows origin to correspond to the bounding box origin
  939.       contained in the placeable header */
  940.    SetWindowOrg(hDC, ahdr.bbox.left, ahdr.bbox.top);
  941.  
  942.    /* set the window extents based on the abs value of the bbox coords */
  943.    SetWindowExt(hDC, abs(ahdr.bbox.left) + abs(ahdr.bbox.right), abs(ahdr.bbox.
  944.                 top) + abs(ahdr.bbox.bottom));
  945.  
  946.    /* set the viewport origin and extents */
  947.    if (nDest != WMFPRINTER)
  948.    {
  949.       SetViewportOrg(hDC, 0, 0);
  950.       SetViewportExt(hDC, rect.right, rect.bottom);
  951.    }
  952.    else
  953.    {
  954.       SetViewportOrg(hPr, 0, 0);
  955.       SetViewportExt(hPr, GetDeviceCaps(hPr, HORZRES), GetDeviceCaps(hPr,
  956.                      VERTRES));
  957.    }
  958. }
  959.  
  960. /***********************************************************************
  961.  
  962.   FUNCTION   : SetClipMetaExts
  963.  
  964.   PARAMETERS : HDC          hDC
  965.                METAFILEPICT MFP
  966.                int          nDest
  967.  
  968.   PURPOSE    : set the extents to the client rect for clipboard metafiles
  969.  
  970.   CALLS      : WINDOWS
  971.                  GetClientRect
  972.                  IntersectClipRect
  973.                  SetMapMode
  974.                  SetViewportOrg
  975.                  SetViewportExt
  976.                  SetWindowExt
  977.  
  978.   MESSAGES   : none
  979.  
  980.   RETURNS    : void
  981.  
  982.   COMMENTS   : this is not as robust as it could be.  A more complete
  983.                approach might be something like Petzold discusses in
  984.                his Programming Windows book on page 793 in the
  985.                function PrepareMetaFile().
  986.  
  987.   HISTORY    : 1/16/91 - created - drc
  988.  
  989. ************************************************************************/
  990.  
  991.  
  992. void SetClipMetaExts (hDC, MFP, nDest)
  993. HDC hDC;
  994. METAFILEPICT MFP;
  995. int nDest;
  996. {
  997.    int cx, cy;
  998.    RECT rect;
  999.  
  1000.    /* extents for the display DC */
  1001.  
  1002.    if (nDest != WMFPRINTER)
  1003.    {
  1004.       GetClientRect(hWndMain, &rect);
  1005.       cx = rect.right - rect.left;
  1006.       cy = rect.bottom - rect.top;
  1007.       IntersectClipRect(hDC, rect.left, rect.top, rect.right, rect.bottom);
  1008.    }
  1009.    SetMapMode(hDC, MFP.mm);
  1010.  
  1011.    /* set physical origin to 0, 0 */
  1012.    SetViewportOrg(hDC, 0, 0);
  1013.  
  1014.    /* given the mapping mode specified in the metafilepict */
  1015.    switch (MFP.mm)
  1016.       {
  1017.    case MM_ISOTROPIC:
  1018.       if (MFP.xExt && MFP.yExt)
  1019.          SetWindowExt(hDC, MFP.xExt, MFP.yExt);
  1020.  
  1021.    /* fall through */
  1022.  
  1023.    case MM_ANISOTROPIC:
  1024.       if (nDest != WMFPRINTER)
  1025.          SetViewportExt(hDC, cx, cy);
  1026.       else
  1027.          SetViewportExt(hDC, GetDeviceCaps(hDC, HORZRES), GetDeviceCaps(hDC,
  1028.                         VERTRES));
  1029.       break;
  1030.  
  1031.    default:
  1032.       break;
  1033.       }
  1034. }
  1035.  
  1036. /***********************************************************************
  1037.  
  1038.   FUNCTION   : ProcessFile
  1039.  
  1040.   PARAMETERS : HWND  hWnd
  1041.                LPSTR lpFileName
  1042.  
  1043.   PURPOSE    : open the metafile, determine if it contains a valid
  1044.                metafile, decide what type of metafile it is (wmf,
  1045.                clipboard, or placeable) and take care of some menu
  1046.                housekeeping tasks.
  1047.  
  1048.   CALLS      : WINDOWS
  1049.                  _lopen
  1050.                  _lread
  1051.                  _llseek
  1052.                  _lclose
  1053.                  lstrcmp
  1054.                  lstrcpy
  1055.                  lstrcat
  1056.                  MessageBox
  1057.                  EnableMenuItem
  1058.                  DrawMenuBar
  1059.                  SetWindowText
  1060.  
  1061.                WINCOM
  1062.                  SplitPath
  1063.  
  1064.                APP
  1065.                  RenderPlaceableMeta
  1066.                  RenderClipMeta
  1067.  
  1068.   MESSAGES   : none
  1069.  
  1070.   RETURNS    : BOOL
  1071.  
  1072.   COMMENTS   :
  1073.  
  1074.   HISTORY    : 1/16/91 - created - drc
  1075.  
  1076. ************************************************************************/
  1077.  
  1078.  
  1079. BOOL ProcessFile (hWnd, lpFileName)
  1080. HWND hWnd;
  1081. LPSTR lpFileName;
  1082. {
  1083.    int fh;
  1084.    int wBytesRead;
  1085.    DWORD HeaderPos;
  1086.    DWORD dwIsAldus;
  1087.    CLIPFILEHEADER FileHeader;
  1088.    CLIPFILEFORMAT ClipHeader;
  1089.    char szCaption[144];
  1090.  
  1091.    /* for openfiledialog */
  1092.    char drive[3];
  1093.    char dir[130];
  1094.    char fname[13];
  1095.    char ext[5];
  1096.  
  1097.    /* split the fully qualified filename into its components */
  1098.  
  1099.    SplitPath(lpFileName, (LPSTR)drive, (LPSTR)dir, (LPSTR)fname, (LPSTR)ext);
  1100.  
  1101.    /* if the file is a "traditional" or placeable metafile
  1102.       as per the normal naming conventions */
  1103.    if (lstrcmp((LPSTR)ext, (LPSTR)"WMF") == 0)
  1104.    {
  1105.  
  1106.       /* try to open the file.  It's existence has already been
  1107.          checked by WINCOM's OpenFileDialog */
  1108.       fh = _lopen(lpFileName, OF_READ);
  1109.  
  1110.       /* if opened successfully */
  1111.       if (fh != -1)
  1112.       {
  1113.  
  1114.          /* always disable the clipboard header menu if we get here */
  1115.          EnableMenuItem(GetMenu(hWnd), IDM_CLIPHDR, MF_DISABLED | MF_GRAYED);
  1116.  
  1117.          /* read the first dword of the file to see if it is a placeable wmf */
  1118.          wBytesRead = _lread(fh, (LPSTR)&dwIsAldus, sizeof(dwIsAldus));
  1119.          if (wBytesRead == -1 || wBytesRead < sizeof(dwIsAldus))
  1120.          {
  1121.             _lclose(fh);
  1122.             MessageBox(hWndMain, "unable to read file", NULL, MB_OK |
  1123.                        MB_ICONEXCLAMATION);
  1124.             return (FALSE);
  1125.          }
  1126.  
  1127.          /* if this is windows metafile, not a placeable wmf */
  1128.          if (dwIsAldus != ALDUSKEY)
  1129.          {
  1130.  
  1131.             /* disable aldus header menu item */
  1132.             EnableMenuItem(GetMenu(hWnd), IDM_ALDUSHDR, MF_DISABLED | MF_GRAYED
  1133.                            );
  1134.  
  1135.             /* seek to the beginning of the file */
  1136.             _llseek(fh, 0, 0);
  1137.  
  1138.             /* read the wmf header */
  1139.             wBytesRead = _lread(fh, (LPSTR)&mfHeader, sizeof(METAHEADER));
  1140.  
  1141.             /* done with file so close it */
  1142.             _lclose(fh);
  1143.  
  1144.             /* if read failed */
  1145.             if (wBytesRead == -1 || wBytesRead < sizeof(dwIsAldus))
  1146.             {
  1147.                MessageBox(hWndMain, "unable to read metafile header", NULL,
  1148.                           MB_OK | MB_ICONEXCLAMATION);
  1149.                return (FALSE);
  1150.             }
  1151.          }
  1152.  
  1153.          /* this is a placeable metafile */
  1154.          else
  1155.          {
  1156.  
  1157.             /* enable the placeable header menu item */
  1158.             EnableMenuItem(GetMenu(hWnd), IDM_ALDUSHDR, MF_ENABLED);
  1159.  
  1160.             /* convert the placeable format into something that can
  1161.                be used with GDI metafile functions */
  1162.             RenderPlaceableMeta(fh);
  1163.  
  1164.             /* close the file */
  1165.             _lclose(fh);
  1166.          }
  1167.  
  1168.          /* at this point we have a metafile header regardless of whether
  1169.             the metafile was a windows metafile or a placeable metafile
  1170.             so check to see if it is valid.  There is really no good
  1171.             way to do this so just make sure that the mtType is either
  1172.             1 or 2 (memory or disk file) */
  1173.          if ((mfHeader.mtType != 1) && (mfHeader.mtType != 2))
  1174.          {
  1175.             /* set the program flags appropriately */
  1176.             bBadFile = TRUE;
  1177.             bMetaFileOpen = FALSE;
  1178.             bValidFile = FALSE;
  1179.  
  1180.             /* let the user know that this is an invalid metafile */
  1181.             MessageBox(hWndMain, "This file is not a valid metafile", NULL,
  1182.                        MB_OK | MB_ICONEXCLAMATION);
  1183.  
  1184.             /* restore the caption text to the default */
  1185.             SetWindowText(hWnd, (LPSTR)APPNAME);
  1186.  
  1187.             /* disable menu items, indicating that a valid metafile has not been
  1188.                loaded */
  1189.             EnableMenuItem(GetMenu(hWnd), IDM_VIEW, MF_DISABLED | MF_GRAYED |
  1190.                            MF_BYPOSITION);
  1191.             EnableMenuItem(GetMenu(hWnd), IDM_PLAY, MF_DISABLED | MF_GRAYED |
  1192.                            MF_BYPOSITION);
  1193.             EnableMenuItem(GetMenu(hWnd), IDM_PRINT, MF_DISABLED | MF_GRAYED);
  1194.  
  1195.             /* refresh the menu bar to reflect above changes */
  1196.             DrawMenuBar(hWnd);
  1197.          }
  1198.          /* this is a valid metafile...at least based on the above criteria */
  1199.          else
  1200.          {
  1201.  
  1202.             /* modify and update the caption text */
  1203.             wsprintf((LPSTR)szCaption, (LPSTR)"%s - %s.%s", (LPSTR)APPNAME, (
  1204.                      LPSTR)fname, (LPSTR)ext);
  1205.  
  1206.             /* this could be used by the printing routines if unable to print */
  1207.             wsprintf((LPSTR)fnameext, (LPSTR)"%s.%s", (LPSTR)fname, (LPSTR)ext)
  1208.             ;
  1209.             SetWindowText(hWnd, (LPSTR)szCaption);
  1210.  
  1211.             /* enable the appropriate menu items */
  1212.             EnableMenuItem(GetMenu(hWnd), IDM_VIEW, MF_ENABLED | MF_BYPOSITION)
  1213.             ;
  1214.             EnableMenuItem(GetMenu(hWnd), IDM_PLAY, MF_ENABLED | MF_BYPOSITION)
  1215.             ;
  1216.             EnableMenuItem(GetMenu(hWnd), IDM_PRINT, MF_ENABLED);
  1217.  
  1218.             /* refresh the menu bar to reflect above changes */
  1219.             DrawMenuBar(hWnd);
  1220.  
  1221.             /* set program flags appropriately */
  1222.             bValidFile = TRUE;
  1223.             bMetaFileOpen = TRUE;
  1224.             if (dwIsAldus != ALDUSKEY)
  1225.             {
  1226.                bAldusMeta = FALSE;
  1227.                bMetaInRam = FALSE;
  1228.             }
  1229.             else
  1230.             {
  1231.                bAldusMeta = TRUE;
  1232.                bMetaInRam = TRUE;
  1233.             }
  1234.          }
  1235.          return (TRUE);
  1236.       } /* if fh != -1 */
  1237.       else
  1238.          return (FALSE);
  1239.    }
  1240.    /* file ext is not WMF so check to see if it is a clipboard file */
  1241.    else
  1242.    {
  1243.       if (lstrcmp((LPSTR)ext, (LPSTR)"CLP") == 0)
  1244.       {
  1245.          WORD i;
  1246.  
  1247.          /* try to open the file.  It's existence has already been
  1248.             checked by WINCOM's OpenFileDialog */
  1249.  
  1250.          fh = _lopen(lpFileName, OF_READ);
  1251.  
  1252.          /* if opened successfully */
  1253.          if (fh != -1)
  1254.          {
  1255.  
  1256.             /* read the clipboard file header */
  1257.             FileHeader.FormatCount = 0;
  1258.             _lread(fh, (LPSTR)&FileHeader, sizeof(CLIPFILEHEADER));
  1259.  
  1260.             /* if this is not a valid clipboard file based on the file
  1261.                identifier of the file header */
  1262.             if (FileHeader.FileIdentifier != CLP_ID)
  1263.             {
  1264.                _lclose(fh);
  1265.                MessageBox(hWndMain, "This file is not a valid clipboard file",
  1266.                           NULL, MB_OK | MB_ICONEXCLAMATION);
  1267.                return (FALSE);
  1268.             }
  1269.             HeaderPos = sizeof(CLIPFILEHEADER);
  1270.  
  1271.             /* search the formats contained within the clipboard file looking
  1272.                for a metafile.  Break if and when it is found */
  1273.             for (i = 0; i < FileHeader.FormatCount; i++)
  1274.             {
  1275.                _llseek(fh, HeaderPos, 0);
  1276.  
  1277.                /* read the clipboard header found at current position */
  1278.                if (_lread(fh, (LPSTR)&ClipHeader, sizeof(ClipHeader)) < sizeof(
  1279.                    ClipHeader))
  1280.                {
  1281.                   _lclose(fh);
  1282.                   MessageBox(hWndMain, "read of clipboard header failed", NULL,
  1283.                              MB_OK | MB_ICONEXCLAMATION);
  1284.                   return (FALSE);
  1285.                }
  1286.  
  1287.                /* increment the file offset */
  1288.                HeaderPos += sizeof(ClipHeader);
  1289.  
  1290.                /* if a metafile was found break */
  1291.                if (ClipHeader.FormatID == CF_METAFILEPICT)
  1292.                   break;
  1293.             }
  1294.  
  1295.             /* was it really so? */
  1296.             if (ClipHeader.FormatID == CF_METAFILEPICT)
  1297.             {
  1298.  
  1299.                /* if there is currently a metafile loaded delete it */
  1300.                if ((bMetaInRam) && (hMF))
  1301.                   DeleteMetaFile(hMF);
  1302.  
  1303.                /* modify and update the caption text */
  1304.                wsprintf((LPSTR)szCaption, (LPSTR)"%s - %s.%s", (LPSTR)APPNAME,
  1305.                         (LPSTR)fname, (LPSTR)ext);
  1306.  
  1307.                /* this could be used by the printing routines if unable to print */
  1308.                wsprintf((LPSTR)fnameext, (LPSTR)"%s.%s", (LPSTR)fname, (LPSTR)
  1309.                         ext);
  1310.                SetWindowText(hWnd, (LPSTR)szCaption);
  1311.  
  1312.                /* enable the appropriate menu items */
  1313.                EnableMenuItem(GetMenu(hWnd), IDM_ALDUSHDR, MF_DISABLED |
  1314.                               MF_GRAYED);
  1315.                EnableMenuItem(GetMenu(hWnd), IDM_CLIPHDR, MF_ENABLED);
  1316.                EnableMenuItem(GetMenu(hWnd), IDM_VIEW, MF_ENABLED |
  1317.                               MF_BYPOSITION);
  1318.                EnableMenuItem(GetMenu(hWnd), IDM_PLAY, MF_ENABLED |
  1319.                               MF_BYPOSITION);
  1320.                EnableMenuItem(GetMenu(hWnd), IDM_PRINT, MF_ENABLED);
  1321.  
  1322.                /* refresh the menu bar */
  1323.                DrawMenuBar(hWnd);
  1324.  
  1325.                /* set the program flags appropriately */
  1326.                bValidFile = TRUE;
  1327.                bMetaFileOpen = TRUE;
  1328.                bMetaInRam = TRUE;
  1329.                bAldusMeta = FALSE;
  1330.  
  1331.                /* convert the metafile contained within the clipboard file into
  1332.                   a format useable with GDI metafile functions */
  1333.                if (!RenderClipMeta(&ClipHeader, fh))
  1334.                   MessageBox(hWndMain, "Unable to render format", NULL, MB_OK |
  1335.                              MB_ICONEXCLAMATION);
  1336.  
  1337.                /* close the file */
  1338.                _lclose(fh);
  1339.             }
  1340.             /* a metafile was not found within the clipboard file */
  1341.             else
  1342.             {
  1343.                bBadFile = TRUE;
  1344.                bMetaFileOpen = FALSE;
  1345.                bValidFile = FALSE;
  1346.  
  1347.                /* let the user know */
  1348.                MessageBox(hWndMain,
  1349.                           "This CLP file doesn't contain a valid metafile",
  1350.                           NULL, MB_OK | MB_ICONEXCLAMATION);
  1351.  
  1352.                /* restore the caption text to default */
  1353.                SetWindowText(hWnd, (LPSTR)APPNAME);
  1354.  
  1355.                /* disable previously enabled menu items */
  1356.                EnableMenuItem(GetMenu(hWnd), IDM_VIEW, MF_DISABLED | MF_GRAYED
  1357.                               | MF_BYPOSITION);
  1358.                EnableMenuItem(GetMenu(hWnd), IDM_PLAY, MF_DISABLED | MF_GRAYED
  1359.                               | MF_BYPOSITION);
  1360.                EnableMenuItem(GetMenu(hWnd), IDM_PRINT, MF_DISABLED | MF_GRAYED
  1361.                               );
  1362.  
  1363.                /* refresh the menu bar to reflect these changes */
  1364.                DrawMenuBar(hWnd);
  1365.                _lclose(fh);
  1366.             }
  1367.             return (TRUE);
  1368.          }
  1369.          else
  1370.             return (FALSE);
  1371.       }
  1372.    }
  1373. }
  1374.